﻿#ifndef DATA_H
#define DATA_H

#include <QSqlDatabase>
#include <QSqlQuery>
#include <QString>
#include <QMessageBox>
#include <QDebug>
#include <QFile>
#include <QTextStream>
#include <QTime>
#include <QDate>
#include <QList>
#include "log/log.h"


class Db_Data: public log
{
public:
    Db_Data();
    QSqlDatabase init_database(QString db_type,QString db_name,QString caller_type);
    bool check_user(QSqlDatabase &db,QString user_name);
    char insert_user_info(QSqlDatabase &db,QString user_name, QString user_password);
    QStringList find_groups(QSqlDatabase &db);
    bool check_table(QSqlDatabase &db,QString table_name);
    bool insert_check(QSqlDatabase &db, QString table ,
                                QString check_row,QString check_data);
    char insert_group_data(QSqlDatabase &db, QString Data);
    char delate_group_data(QSqlDatabase &db, QString Data);
    void insert_person_data(QSqlDatabase &db, QString Data);
    void delate_person_data(QSqlDatabase &db, QString Data);
    char insert_relation_data(QSqlDatabase &db,
                                       QString person,
                                       QString amount,
                                       QString group,
                                       QString date,
                                       QString remark);
    char delete_relation_data_accurate(QSqlDatabase &db,
                                       QString person,
                                       QString amount,
                                       QString group,
                                       QString date,
                                       QString remark);
    float count_table_data(QSqlDatabase &db, QString table);
    void delete_relation_data_match(QSqlDatabase &db,QString row_name, QString match_str);
    QList<float> count_amount_all(QSqlDatabase &db);
    QList<float> count_amount_group(QSqlDatabase &db, QString group_name);
    QList<QString> select_relation_items(QSqlDatabase &db, QList<QString> row_name, QList<QString> condition);
    float count_amount_date(QSqlDatabase &db, QString date_begin, QString date_end);
    enum{
        I_EXIST = 0, //插入信息时已经存在
        I_NOT_EXIST , //插入信息时已经存在
        I_FALSE,   //插入信息失败
        I_SUCCESS,  //插入信息成功
    }Insert_INFO;
private:
    //数据库创建表的基本执行语句
    //用户信息表
    QString CREATE_USER = "create table user_info (id int primary key, name varchar(20),password VARCHAR(8))";

    //数据表
    QString CREATE_PERSON = "create table person(id int primary key, name varchar(20) unique, remark varchar(50))";
    QString CREATE_GROUP = "create table m_group(id int primary key, group_name varchar(20) unique, remark varchar(50))";
    QString CREATE_RELATION = "create table relation(id int primary key, personName varchar(20), amount varchar(10), time date, "
                             "groupName varchar(20), remark varchar(50),"
                             "CONSTRAINT fk_person FOREIGN KEY(personName ) REFERENCES person(name ),"
                             "CONSTRAINT fk_group FOREIGN KEY(groupName ) REFERENCES m_group(group_name ))";
    //查找命令
    QString SELECT_USER = "select name from user_info";
    QString SELECT_PERSON = "select name from person";
    QString SELECT_GROUP = "select group_name from m_group";
};

#endif // DATA_H
